//
//  LKLineView.m
//  YLHelper
//
//  Created by she1323 on 2018/5/22.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKLineView.h"
#define SpotLabelNumber 5

@implementation LKLineView

+(instancetype)createLineViewWithFrame:(CGRect)frame AndLineValues:(NSArray *)lineValues AndXValue:(NSArray *)xvalues AndShowShowRightAxis:(BOOL)showRight AndLinesClors:(NSArray *)lineColors AndLineNames:(NSArray *)lineNames AndDrawFillCorlor:(NSArray *)fillcolorsArray{
    LKLineView *View = [[LKLineView alloc] initWithFrame:frame];
    View.titlesArray = xvalues;
//    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, View.bounds.size.width, 50)];
//    [View addSubview:topView];
//    for (NSInteger i = 0; i < lineNames.count; i++) {
//        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(i *(topView.bounds.size.width / SpotLabelNumber), 0, topView.bounds.size.width / SpotLabelNumber, 50)];
//        [topView addSubview:subView];
//        UIView *spotView = [[UIView alloc] init];
//        spotView.backgroundColor = lineColors[i];
//        [subView addSubview:spotView];
//        [spotView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(10, 10));
//            make.centerX.equalTo(subView);
//            make.centerY.equalTo(subView.mas_centerY).offset(-8);
//        }];
//        spotView.layer.cornerRadius = 5;
//        spotView.layer.masksToBounds = YES;
//        UILabel *titeLabel = [[UILabel alloc] init];
//        titeLabel.textAlignment = NSTextAlignmentCenter;
//        titeLabel.text = lineNames[i];
//        titeLabel.font = [UIFont systemFontOfSize:10];
//        titeLabel.textColor = COLOR_HEX(0x3B3B3C);
//        [subView addSubview:titeLabel];
//        [titeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(subView);
//            make.top.equalTo(spotView.mas_bottom).offset(6);
//        }];
//
//    }
    
    LineChartView *lineChartV = [[LineChartView alloc] initWithFrame:CGRectMake(0, 50, View.bounds.size.width, View.bounds.size.height - 50)];
    lineChartV.chartDescription.enabled = NO;
    [View addSubview:lineChartV];
    NSMutableArray *alllineValues = [[NSMutableArray alloc] init];
    double rightSetMin = 0;
    double rightSetMax = 0;
    double leftSetMin = 0;
    double leftSetMax = 0;
    for (NSInteger i = 0; i < lineValues.count; i++) {
        NSArray *linearray = lineValues[i];
        NSMutableArray *lineValls = [[NSMutableArray alloc] init];
        for (int j = 0; j < linearray.count; j++)
        {
            NSString *valStr = [NSString stringWithFormat:@"%@", linearray[j]];
            double val = [valStr doubleValue];
            leftSetMax = MAX(val, leftSetMax);
            leftSetMin = MIN(val, leftSetMin);
            ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:j  y:val];

            [lineValls addObject:entry];
        }
        [alllineValues addObject:lineValls];
    }
    NSMutableArray *linedateSets = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < alllineValues.count; i++) {
        NSArray *lineVals = [alllineValues objectAtIndex:i];
        LineChartDataSet *lineSet = [[LineChartDataSet alloc] initWithValues:lineVals label:lineNames[i]];
//        lineSet.mode = LineChartModeHorizontalBezier;
        lineSet.lineWidth = 3.0f;
        lineSet.drawValuesEnabled = YES;
        lineSet.valueColors = @[[UIColor redColor]];
        [lineSet setColor:lineColors[i]];
        lineSet.drawCirclesEnabled = YES;
        [lineSet setCircleColor:blueDefault];
        lineSet.circleRadius = 3.0f;
        lineSet.drawCircleHoleEnabled = YES;
        lineSet.circleHoleRadius = 5.0f;
        lineSet.circleHoleColor = blueDefault;
        lineSet.highlightEnabled = NO;
        lineSet.fillColor = fillcolorsArray[i];
        lineSet.drawFilledEnabled = YES;
        lineSet.valueFont = [UIFont systemFontOfSize:12];
        lineSet.axisDependency = AxisDependencyLeft;
        
        NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
        customFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
        customFormatter.maximumFractionDigits = 2;
//        customFormatter.minimumFractionDigits = 2;
        lineSet.valueFormatter = [[ChartDefaultValueFormatter alloc] initWithFormatter:customFormatter];
        [linedateSets addObject:lineSet];
    }
    double leftDiff = leftSetMax - leftSetMin;
    
    if (leftSetMax == 0 && leftSetMin == 0) {
        leftSetMax = 100;
        leftSetMin = 0;
    } else {
        leftSetMax = (leftSetMax + leftDiff * 0.1);
        leftSetMin = (leftSetMin - leftDiff * 0.1);
    }
    lineChartV.leftAxis.axisMaximum = leftSetMax;
    lineChartV.leftAxis.axisMinimum = 0;
    lineChartV.leftAxis.labelCount = 10;
    
//    NSNumberFormatter *yFormatter = [[NSNumberFormatter alloc] init];
//    yFormatter.numberStyle = kCFNumberFormatterNoStyle;
//    lineChartV.leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:yFormatter];
    
    
    ChartXAxis *xAxis = lineChartV.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:10];
    xAxis.labelCount = xvalues.count;
    xAxis.drawLabelsEnabled = YES;
//    xAxis.labelRotationAngle = -40;
    xAxis.yOffset = 10;
    xAxis.labelTextColor = blueDefault;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.forceLabelsEnabled = NO;
    xAxis.valueFormatter = View;
    xAxis.granularity = 1.00;
    //设置图例
    ChartLegend *legend = lineChartV.legend;
    legend.enabled = NO;
//    legend.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
//    legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
//    legend.orientation = ChartLegendOrientationHorizontal;
////    legend.drawInside = YES;
//    legend.direction = ChartLegendDirectionLeftToRight;
//    legend.form = ChartLegendFormCircle;
//    legend.formSize = 10;
//    legend.font = [UIFont systemFontOfSize:10];
//    legend.textColor = COLOR_HEX(0x595959);
////    legend.yOffset = -10;
//    legend.yOffset = 10;
    //左侧Y轴设置
    ChartYAxis *leftAxis = lineChartV.leftAxis;
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.axisMinimum = 0;
    leftAxis.labelTextColor = blueDefault;
    leftAxis.drawAxisLineEnabled = YES;
    leftAxis.drawGridLinesEnabled = YES;
    
    lineChartV.rightAxis.labelCount = 0;
    lineChartV.rightAxis.drawGridLinesEnabled = NO;
    lineChartV.rightAxis.drawAxisLineEnabled = NO;
    
//    leftAxis.drawTopYLabelEntryEnabled = YES;
    //    lineChartV.rightAxis.labelFont = [UIFont systemFontOfSize:0.0];
    lineChartV.rightAxis.labelTextColor = [UIColor clearColor];
    LineChartData *lineData = [[LineChartData alloc] initWithDataSets:linedateSets];
    lineChartV.data = lineData;
    lineChartV.xAxis.axisMinimum = lineChartV.data.xMin - 0.5;
    lineChartV.xAxis.axisMaximum = lineChartV.data.xMax + 0.5;
    [lineChartV animateWithXAxisDuration:0.8 easingOption:ChartEasingOptionLinear];
    
//    [lineChartV animateWithXAxisDuration:3 yAxisDuration:3 easing:ChartEasingOptionLinear];
//    [lineChartV animateWithXAxisDuration:1 yAxisDuration:1];
//    [lineChartV animateWithXAxisDuration:1 easingOption:ChartEasingOptionLinear];
    return View;
}

#pragma mark - IChartValueFormatter delegate (折线值)

//- (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler {
//     return [NSString stringWithFormat:@"%.5lf", value];
//    
//}
#pragma mark - IAxisValueFormatter

- (NSString *)stringForValue:(double)value
                        axis:(ChartAxisBase *)axis
{
    return self.titlesArray[(int)value];
}


@end
